//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using LitwareHR.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.EventBroker;
using LitwareHR.PublicApp.WinUX.Constants;
using LitwareHR.PublicApp.WinUX.Services;
using System.Globalization;
using LitwareHR.PublicApp.WinUX.Properties;
  
namespace LitwareHR.PublicApp.WinUX
{
    public partial class LoginViewPresenter : Presenter<ILoginView>
    {
        /// <summary>
        /// Authentication Service
        /// </summary>
        ServiceProxy.UnSecure.AuthenticationService authService = null;
        ServiceProxy.UnSecure.SystemService systemService = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="authService"></param>
        [InjectionConstructor]
        public LoginViewPresenter([ServiceDependency] ServiceProxy.UnSecure.AuthenticationService authService,
                                  [ServiceDependency] ServiceProxy.UnSecure.SystemService  systemService)
        {
            this.authService = authService;
            this.systemService = systemService;
        }

        /// <summary>
        /// This method is a placeholder that will be called by the view when it has been loaded.
        /// </summary>
        public override void OnViewReady()
        {
            base.OnViewReady();
            View.LoadResources();         
        }

        /// <summary>
        /// Close the view
        /// </summary>
        public void OnCloseView()
        {
            base.CloseView();
            WorkItem.SmartParts.Remove(View);
            if (View is IDisposable)
                ((IDisposable)View).Dispose();
        }

        /// <summary>
        /// Validate User, store tenantAlias, tenatId, SecurityToken offline
        /// </summary>
        /// <param name="tenantAlias"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        public void ValidateUser(string tenantAlias, string user, string password)
        {
            if (authService.ValidateUser(tenantAlias, user, password))
            {
                using (SystemServiceChannel channel = new SystemServiceChannel())
                {
                    string upn = string.Format(CultureInfo.InvariantCulture, "{0}@{1}", user, tenantAlias);
                    channel.ClientCredentials.UserName.UserName = upn;
                    channel.ClientCredentials.UserName.Password = password;
                    channel.Ping();                  
                    Settings.Default.TenantAlias = tenantAlias;
                    Settings.Default.TenantId = systemService.GetTenantId(tenantAlias).ToString();
                    Settings.Default.UserName = user;
                    string[] roles = authService.GetRolesForUser(tenantAlias, user);
                    Settings.Default.Roles = string.Join("~", roles);
                    Settings.Default.Save();                    
                }

                OnCloseView();
            }
            else
            {
                View.ShowMessage(Resources.InvalidUserName);  
            }
        }
    }
}

